package com.ruoyi.Outsider.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Outsider.domain.Outsider;
import com.ruoyi.Outsider.service.IOutsiderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.Outsider.ali.SendMessage.sendSms;
import static com.ruoyi.Outsider.ali.SendMessage.sendSms1;

/**
 * 外来人口访问登记Controller
 * 
 * @author 张柏焰
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/Outsiders/Outsiders")
public class OutsiderController extends BaseController
{
    @Autowired
    private IOutsiderService outsiderService;

    /**
     * 查询外来人口访问登记列表
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Outsider outsider)
    {
        startPage();
        List<Outsider> list = outsiderService.selectOutsiderList(outsider);
        return getDataTable(list);
    }

    /**
     * 导出外来人口访问登记列表
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:export')")
    @Log(title = "外来人口访问登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Outsider outsider)
    {
        List<Outsider> list = outsiderService.selectOutsiderList(outsider);
        ExcelUtil<Outsider> util = new ExcelUtil<Outsider>(Outsider.class);
        util.exportExcel(response, list, "外来人口访问登记数据");
    }

    /**
     * 获取外来人口访问登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(outsiderService.selectOutsiderById(id));
    }

    /**
     * 新增外来人口访问登记
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:add')")
    @Log(title = "外来人口访问登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Outsider outsider)
    {
        return toAjax(outsiderService.insertOutsider(outsider));
    }

    /**
     * 修改外来人口访问登记
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:edit')")
    @Log(title = "外来人口访问登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Outsider outsider) throws ClientException {
        if (outsider.getPermit()==0){
            //发短信
            SendSmsResponse response = sendSms(outsider.getPhone());
            System.out.println("短信接口返回的数据----------------");
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        }else if (outsider.getPermit()==1){
            SendSmsResponse response = sendSms1(outsider.getPhone());
            System.out.println("短信接口返回的数据----------------");
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        }



        return toAjax(outsiderService.updateOutsider(outsider));
    }

    /**
     * 删除外来人口访问登记
     */
    @PreAuthorize("@ss.hasPermi('Outsiders:Outsiders:remove')")
    @Log(title = "外来人口访问登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(outsiderService.deleteOutsiderByIds(ids));
    }
}
